package com.xq.shop.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xq.shop.dao.IReceiverDAO;
import com.xq.shop.po.Receiver;
import com.xq.shop.util.HibernateUtil;

@Repository
@SuppressWarnings("unchecked")
public class ReceiverDAO extends BaseDAO<Receiver> implements IReceiverDAO {

	@Override
	public void update(Receiver obj) {
		
		String hql = "update Receiver set contact=:contact, address=:address, postcode=:postcode," +
				" tel=:tel, is_default=:is_default where id=:id and user_id=:user_id";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", obj.getId());
		param.put("user_id", obj.getUser_id());
		param.put("contact", obj.getContact());
		param.put("address", obj.getAddress());
		param.put("postcode", obj.getPostcode());
		param.put("tel", obj.getTel());
		param.put("is_default", obj.getIs_default());
		HibernateUtil.execute(hql, param);
	}

	@Override
	public List<Receiver> getByUserID(Long id) {
		
		String hql = " from Receiver where user_id=:user_id";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user_id", id);
		return (List<Receiver>) HibernateUtil.executeFind(hql, param);
	}

	@Override
	public void delete(Long id, Long userId) {
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		param.put("user_id", userId);
		HibernateUtil.execute("delete Receiver where id=:id and user_id=:user_id)", param);
	}
}
